package springboot.bigdata.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springboot.bigdata.demo.dao.*;
import springboot.bigdata.demo.example.old_db.*;
import springboot.bigdata.demo.mappers.old_db.*;
import springboot.bigdata.demo.model.*;
import springboot.bigdata.demo.model.old_db.*;
import springboot.bigdata.demo.service.DataTransitionService;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DataTransitionServiceImpl implements DataTransitionService {

    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private UserDao userDao;
    @Autowired
    private ExpenseMapper expenseMapper;
    @Autowired
    private UserExpenseDao userExpenseDao;
    @Autowired
    private ServiceItemConfigMapper serviceItemConfigMapper;
    @Autowired
    private UserItemConfigDao userItemConfigDao;
    @Autowired
    private StaffServiceItemMapper staffServiceItemMapper;
    @Autowired
    private UserServiceItemDao userServiceItemDao;
    @Autowired
    private StaffServiceLevelChangeRecordMapper staffServiceLevelChangeRecordMapper;
    @Autowired
    private UserServiceLevelChangeRecordDao userServiceLevelChangeRecordDao;

    @Override
    public void staffToUser() {

        int allTotal = staffMapper.getAllTotal();

        int pageNum = 1;
        int pageSize = 1000;
        int totalPageNum = countTotalPageNum(allTotal, pageSize);

        List<User> db0List = new ArrayList<>();
        List<User> db1List = new ArrayList<>();

        while (pageNum <= totalPageNum){
            PageHelper.startPage(pageNum, pageSize);
            pageNum++;

            List<Staff> staffList = staffMapper.selectByExample(new StaffExample());
            staffList.stream().forEach(staff -> {
                User userPar = new User();
                BeanUtil.copyProperties(staff, userPar);
                userPar.setUserId(staff.getStaffId())
                        .setUserName(staff.getStaffName())
                        .setUserNumber(staff.getStaffNumber());

                if (userPar.getUserId() %2 == 0){
                    db0List.add(userPar);
                }else {
                    db1List.add(userPar);
                }

                if (db0List.size() >= 1000){
                    userDao.addUserList(db0List, 0);
                    db0List.clear();
                }else if (db1List.size() >= 1000){
                    userDao.addUserList(db1List, 1);
                    db1List.clear();
                }
            });
        }

        userDao.addUserList(db0List, 0);
        userDao.addUserList(db1List, 1);
    }

    @Override
    public void itemConfigToNewDb() {

        int allTotal = serviceItemConfigMapper.getAllTotal();

        int pageNum = 1;
        int pageSize = 1000;
        int totalPageNum = countTotalPageNum(allTotal, pageSize);

        while (pageNum <= totalPageNum){
            PageHelper.startPage(pageNum, pageSize);
            pageNum++;

            List<ServiceItemConfig> configList = serviceItemConfigMapper.selectByExample(new ServiceItemConfigExample());
            configList.stream().forEach(config -> {
                UserItemConfig itemConfig = new UserItemConfig();
                BeanUtil.copyProperties(config, itemConfig);
                userItemConfigDao.add(itemConfig);
            });
        }

    }

    @Override
    public void serviceItemToNewDb() {

        int allTotal = staffServiceItemMapper.getAllTotal();

        int pageNum = 1;
        int pageSize = 1000;
        int totalPageNum = countTotalPageNum(allTotal, pageSize);

        List<UserServiceItem> db0List = new ArrayList<>();
        List<UserServiceItem> db1List = new ArrayList<>();

        while (pageNum <= totalPageNum){
            PageHelper.startPage(pageNum, pageSize);
            pageNum++;

            List<StaffServiceItem> itemList = staffServiceItemMapper.selectByExample(new StaffServiceItemExample());
            itemList.stream().forEach(item -> {
                UserServiceItem userServiceItem = new UserServiceItem();
                BeanUtil.copyProperties(item, userServiceItem);
                userServiceItem.setUserId(item.getStaffId());

                if (item.getServiceItemId() %2 == 0){
                    db0List.add(userServiceItem);
                }else {
                    db1List.add(userServiceItem);
                }

                if (db0List.size() >= 1000){
                    userServiceItemDao.addList(db0List, 0);
                    db0List.clear();
                }else if (db1List.size() >= 1000){
                    userServiceItemDao.addList(db1List, 1);
                    db1List.clear();
                }
            });
        }

        userServiceItemDao.addList(db0List, 0);
        userServiceItemDao.addList(db1List, 1);
    }

    @Override
    public void expenseToNewDb() {

        int allTotal = expenseMapper.getAllTotal();
        int pageNum = 1;
        int pageSize = 1000;
        int totalPageNum = countTotalPageNum(allTotal, pageSize);

        List<UserExpense> db0List = new ArrayList<>();
        List<UserExpense> db1List = new ArrayList<>();

        while (pageNum <= totalPageNum){
            PageHelper.startPage(pageNum, pageSize);
            pageNum++;

            List<Expense> expenseList = expenseMapper.selectByExample(new ExpenseExample());
            expenseList.stream().forEach(expense -> {
                if (expense.getUserType() == 1){
                    UserExpense userExpensePar = new UserExpense();
                    BeanUtil.copyProperties(expense, userExpensePar);
                    userExpensePar.setUserId(expense.getSourceId());

                    if (userExpensePar.getExpenseId() %2 == 0){
                        db0List.add(userExpensePar);
                    }else {
                        db1List.add(userExpensePar);
                    }

                    if (db0List.size() >= 1000){
                        userExpenseDao.addList(db0List, 0);
                        db0List.clear();
                    }else if (db1List.size() >= 1000){
                        userExpenseDao.addList(db1List, 1);
                        db1List.clear();
                    }
                }
            });
        }
        userExpenseDao.addList(db0List, 0);
        userExpenseDao.addList(db1List, 1);
    }

    @Override
    public void serviceLevenToNewDb() {

        int allTotal = staffServiceLevelChangeRecordMapper.getAllTotal();

        int pageNum = 1;
        int pageSize = 1000;
        int totalPageNum = countTotalPageNum(allTotal, pageSize);

        List<UserServiceLevelChangeRecord> db0List = new ArrayList<>();
        List<UserServiceLevelChangeRecord> db1List = new ArrayList<>();

        while (pageNum <= totalPageNum){
            PageHelper.startPage(pageNum, pageSize);
            pageNum++;

            List<StaffServiceLevelChangeRecord> recordList = staffServiceLevelChangeRecordMapper.selectByExample(new StaffServiceLevelChangeRecordExample());
            recordList.stream().forEach(record -> {
                UserServiceLevelChangeRecord levelChangeRecord = new UserServiceLevelChangeRecord();
                BeanUtil.copyProperties(record, levelChangeRecord);
                levelChangeRecord.setUserId(record.getStaffId());

                if (levelChangeRecord.getChangeRecordId() %2 == 0){
                    db0List.add(levelChangeRecord);
                }else {
                    db1List.add(levelChangeRecord);
                }

                if (db0List.size() >= 1000){
                    userServiceLevelChangeRecordDao.addList(db0List, 0);
                    db0List.clear();
                }else if (db1List.size() >= 1000){
                    userServiceLevelChangeRecordDao.addList(db1List, 1);
                    db1List.clear();
                }
            });
        }
        userServiceLevelChangeRecordDao.addList(db0List, 0);
        userServiceLevelChangeRecordDao.addList(db1List, 1);
    }

    /**
     * 通过总记录数和每页大小计算总页数
     * @param allTotal
     * @param pageSize
     * @return
     */
    private int countTotalPageNum(int allTotal, int pageSize){
        int totalPageNum = allTotal / pageSize == 0 ? allTotal / pageSize : allTotal / pageSize + 1;
        if (totalPageNum == 0) {
            totalPageNum = 1;
        }
        return totalPageNum;
    }
}
